package de.hska.master.support;

import de.hska.master.model.Point;

/**
 * This class represents a mathematical vector
 * 
 * @author claudio
 * 
 */
public class Vector {
	private double x = 0;
	private double y = 0;

	/**
	 * create vector based on two points
	 * 
	 * @param start
	 *            point
	 * @param end
	 *            point
	 */
	public Vector(Point start, Point end) {
		this.x = (end.getX() - start.getX());
		this.y = (end.getY() - start.getY());
	}

	/**
	 * create vector based on a points
	 * 
	 * @param asPoint
	 */
	public Vector(Point asPoint) {
		this.x = asPoint.getX();
		this.y = asPoint.getY();
	}

	/**
	 * create vector based on two doubles (2D)
	 * 
	 * @param x
	 * @param y
	 */
	public Vector(double x, double y) {
		this.x = x;
		this.y = y;
	}

	// public Vector add(Vector v) {
	// return new Vector(this.x + v.getX(), this.y + v.getY());
	// }

	/**
	 * substract vector v from this and return solution as new vector
	 * 
	 * @param v
	 * @return
	 */
	public Vector sub(Vector v) {
		return new Vector((this.x - v.getX()), (this.y - v.getY()));
	}

	/**
	 * multiply vector v from this and return solution as new vector
	 * 
	 * @param m
	 * @return
	 */
	public Vector mul(double m) {
		return new Vector(this.x * m, this.y * m);
	}

	/**
	 * divide vector v from this and return solution as new vector
	 * 
	 * @param d
	 * @return
	 */
	public Vector div(double d) {
		return new Vector(this.x / d, this.y / d);
	}

	/**
	 * return length of vector
	 * 
	 * @return
	 */
	public double length() {
		return Math.sqrt(x * x + y * y);
	}

	// public double lengthToPointSquared(Point p1) {
	// double deltaX = p1.getX() - this.getY();
	// double deltaY = p1.getY() - this.getY();
	//
	// double lengthOfLineSquared = Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
	// return lengthOfLineSquared;
	// }
	//
	// public double lengthToPoint(Point p1) {
	// double lengthOfLine = Math.sqrt(this.lengthToPointSquared(p1));
	// return lengthOfLine;
	// }

	/**
	 * returns x component of vector
	 * 
	 * @return
	 */
	public double getX() {
		return x;
	}

	/**
	 * returns y component of vector
	 * 
	 * @return
	 */
	public double getY() {
		return y;
	}

	/**
	 * normalizes vector, by dividing its scalars by its length. A new vector object is returned
	 * 
	 * @return normalised vector
	 */
	public Vector normalize() {
		return div(length());
	}

	/**
	 * calculates and returns the scalar product of this vector and the parameter n
	 * 
	 * @param n
	 *            another vector
	 * @return scalar product
	 */
	public double scalarProduct(Vector n) {
		return (this.x * n.getX() + this.y * n.getY());
	}

}
